package com.demo.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import com.demo.constant.ErrorInfo;
import com.demo.dto.AccessToken;
import com.demo.kit.StrKit;
import com.demo.model.User;
import com.demo.validator.user.GetOrDel;
import com.demo.validator.user.Login;
import com.demo.validator.user.addOrUpdate;
import com.jfinal.aop.Before;
import com.jfinal.kit.Kv;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jfinal.plugin.redis.Redis;

public class UserController extends BaseController {
	public static Log log = Log.getLog(UserController.class);
	// 获取验证码
	public void getCode(){
		renderCaptcha();
	}
	@Before(Login.class)
	public void login() throws UnsupportedEncodingException{
		User user = getModel(User.class, "");
		// 校验验证码
	/*	if (!validateCaptcha("code")) {
			renderDataJson(ErrorInfo.ERR_CODE_ERROR);
			return;
		}*/
		user = user.checkLogin();
		if (user == null) {
			renderDataJson(ErrorInfo.ERR_WRONG_USERNAME_OR_PWD);
			return;
		}
		AccessToken accessToken = new AccessToken(String.valueOf(user.getInt("id")), StrKit.getTime());
		String token = accessToken.signature(user.getStr("salt"));
		token = URLEncoder.encode(token, "utf-8");
		user.put("access_token", token);
		renderDataJson(ErrorInfo.ERR_SUCCESS, user);
	}
	public void test(){
//		Kv cond = Kv.by("username", "zhoufan").set("password", "123");
//		SqlPara sqlPara = Db.getSqlPara("user.checkLogin", cond);
//		System.out.println(sqlPara);
//		renderDataJson(ErrorInfo.ERR_SUCCESS, Db.findFirst(sqlPara));
//		User user = getModel(User.class, "user");
//		int page = getParaToInt("page", 1);
//		int pageSize = getParaToInt("pageSize", 10);
//		Kv cond = Kv.by("id =", 15);
//		System.out.println(cond);
//		Kv.fail("user", user);
//		System.out.println(Kv.fail("user", user));
//		Record record = new Record();
//		record.setColumns(user);
//		System.out.println(record.toString());
//		renderDataJson(ErrorInfo.ERR_SUCCESS, record);
//		String tableName = "user";
//		Kv cond = Kv.by("id =", 26003);
//		List<String> orderCond = Arrays.asList("id asc", "createtime desc");		
//		SqlPara sqlPara = Db.getSqlPara("common.findByParamsAndOrder",
//				Kv.by("cond", cond).set("tableName", tableName).set("orderCond", orderCond));
//		System.out.println(sqlPara);
//		renderDataJson(ErrorInfo.ERR_SUCCESS, Db.paginate(1, 10, sqlPara));		
	}

	@Before(addOrUpdate.class)
	public void add() {
		User user = getModel(User.class, "");
		if (user.checkUserName(user.getUserName())) {
			renderDataJson(ErrorInfo.ERR_USERNAME_NOT_DUPLICATE);
			return;
		}
		user.set("salt", StrKit.getRandomUUID());
		user.set("status", User.STATUS_OK);
		if (!user.save()) {
			renderDataJson(ErrorInfo.ERR_FAIL);
			return;
		}
//		Redis.use("user").del("userList");
		renderDataJson(ErrorInfo.ERR_SUCCESS, User.dao.findById(user.getId()).remove("password", "salt"));
	}

	public void list() {
//		List<User> users = User.dao.listByCache("user", "userList");
		Kv cond = Kv.by("status !=", User.STATUS_LOCK_ID);
		List<Record> users = User.dao.listAll(cond);
		renderDataJson(ErrorInfo.ERR_SUCCESS, User.dao.filter(users));
	}

	@Before(GetOrDel.class)
	public void get() {
		User user = getModel(User.class, "");
		record = user.getById(user.getId());
		if (record == null) {
			renderDataJson(ErrorInfo.ERR_USER_DOES_NOT_EXIST);
			return;
		}
		renderDataJson(ErrorInfo.ERR_SUCCESS, record.remove("password", "salt"));
	}

	@Before(addOrUpdate.class)
	public void update() {
		User user = getModel(User.class, "");
		record = user.getById(user.getId());
		if (record == null) {
			renderDataJson(ErrorInfo.ERR_USER_DOES_NOT_EXIST);
			return;
		}
		if (!user.update()) {
			renderDataJson(ErrorInfo.ERR_FAIL);
			return;
		}
		renderDataJson(ErrorInfo.ERR_SUCCESS);
	}
	@Before(GetOrDel.class)
	public void delete(){
		User user = getModel(User.class, "");
		record = user.getById(user.getId());
		if (record == null) {
			renderDataJson(ErrorInfo.ERR_USER_DOES_NOT_EXIST);
			return;
		}
		user.set("status", User.STATUS_LOCK_ID);
		if (user.update()) {
			renderDataJson(ErrorInfo.ERR_SUCCESS);
			return;
		}
		renderDataJson(ErrorInfo.ERR_FAIL);
	}
}
